package com.inspur.dgov.vlbod.controller;

import com.inspur.dgov.vlbod.http.DgovResponse;
import com.inspur.dgov.vlbod.service.DemandThumbsUpService;
import com.inspur.dgov.vlbod.service.UserRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 点播点赞服务控制器
 * </p>
 *
 * @author chiyupei
 * @since 2023-05-29
 */
@Slf4j
@RestController
@RequestMapping("/demandThumbs")
public class DemandThumbsUpController {

    @Autowired
    private DemandThumbsUpService demandThumbsUpService;

    @Autowired
    private UserRecordService userRecordService;

    /**
     * 点赞
     *
     * @param demandId 点播id
     * @return rs
     */

    @GetMapping("/add/{id}")
    public DgovResponse putUp(
            HttpServletRequest request,
            @PathVariable("id") String demandId) {
        //获取当前用户
        String userId = userRecordService.getUserRecord(request).getUserId();
        try {
            boolean result = demandThumbsUpService.putUp(demandId, userId);
            return DgovResponse.success(result);
        } catch (Exception exception) {
            log.error("put up failed", exception);
            return DgovResponse.fail("点赞失败");
        }
    }

    /**
     * 取消点赞
     *
     * @param demandId 点播id
     * @return rs
     */
    @GetMapping("/delete/{id}")
    public DgovResponse removeUp(HttpServletRequest request, @PathVariable("id") String demandId) {
        String userId = userRecordService.getUserRecord(request).getUserId();
        try {
            demandThumbsUpService.removeUp(demandId, userId);
            return DgovResponse.success();
        } catch (Exception exception) {
            log.error("remove up failed", exception);
            return DgovResponse.fail("取消点赞失败");
        }
    }

    /**
     * 检查点赞状态
     *
     * @param demandId 点赞id
     * @return 结果
     */
    @GetMapping("/check/{id}")
    public DgovResponse isUp(HttpServletRequest request, @PathVariable("id") String demandId) {
        //获取当前用户
        String userId = userRecordService.getUserRecord(request).getUserId();
        try {
            return DgovResponse.success(demandThumbsUpService.isUp(demandId, userId));
        } catch (Exception exception) {
            log.error("check up failed", exception);
            return DgovResponse.fail("检查点赞失败");
        }

    }


    /**
     * 根据点播id获取点赞总数
     *
     * @param demandId 点播id
     * @return 总数
     */
    @GetMapping("/count/{id}")
    public DgovResponse getUpCount(@PathVariable("id") String demandId) {
        try {
            return DgovResponse.success(demandThumbsUpService.getDemandUpCount(demandId));
        } catch (Exception exception) {
            log.error("get up count failed", exception);
            return DgovResponse.fail("查询失败");
        }
    }

}
